<?php

header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Thu, 1 Jan 1970 01:00:00 GMT"); // Date in the past
//header('Content-type: text/html');
error_reporting(E_ERROR | E_PARSE | E_CORE_ERROR);
ini_set('log_errors', '1');
ini_set('error_log', 'php_error.log');
ini_set('display_errors', '1');


$appIdTMT = "140410776032411";
$appSecretTMT = "fb2894b6ece45f1371ce316818e94a88";

$appId = "55374793287";
$appSecret = "c685ea6a9ea989eff3e5857f0ae7409b";

$host = $_SERVER['HTTP_HOST'];
$uri = trim(dirname($_SERVER['PHP_SELF']), '/\\');

$code = $_REQUEST["code"];
$callback = !empty($_REQUEST['callback']) ? $_REQUEST['callback'] : 'fbAuthCallback';
$scope = !empty($_REQUEST['scope']) ? $_REQUEST['scope'] : 'publish_stream';
$referer = !empty($_REQUEST['referer']) ? $_REQUEST['referer'] : '192.168.1.19';

if ($referer == '192.168.1.19' || $referer == 'localhost')
{
	$myUrl = "http://snalp.org/$uri/fb.authlcl.php"; // Use when run on localhost, this file must be on server to redirect back to localhost
	$returnUrl = !empty($_REQUEST['returnUrl']) ? $_REQUEST['returnUrl'] : "http://$referer/yoshi/yesplaylove.html"; // Use when run on localhost
}
else
{
	$myUrl = "http://$host/$uri/fb.auth.php";
	$returnUrl = !empty($_REQUEST['returnUrl']) ? $_REQUEST['returnUrl'] : "http://$host/yoshi/yesplaylove.html";
}

// Do this first. This sends us to Facebook to get the browser logged in
if (empty($code)) {
	$dialogUrl = "https://www.facebook.com/dialog/oauth?scope=$scope&client_id={$appId}&redirect_uri=" . urlencode($myUrl);
	header("Location: $dialogUrl");
	exit(0);
}

// Do this when returning from Facebook by detecting "code" in the return variables

// Get the access token from Facebook
$token_url = "https://graph.facebook.com/oauth/access_token?client_id={$appId}&redirect_uri=" . urlencode($myUrl) . "&client_secret={$appSecret}&code={$code}";
$accessToken = file_get_contents($token_url);

// Return the access token to the page
$returnFullUrl = "$returnUrl?$accessToken";
header("Location: $returnFullUrl");
?>